package com.jie.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jie.dto.CombinationTopicDTO;
import com.jie.dto.OperationLogDTO;
import com.jie.dto.UserDetailDTO;
import com.jie.entity.CombinationTopic;
import com.jie.entity.OperationLog;
import com.jie.entity.ThematicCategory;
import com.jie.entity.Video;
import com.jie.exception.BizException;
import com.jie.mapper.CombinationTopicMapper;
import com.jie.mapper.ThematicCategoryMapper;
import com.jie.mapper.VideoMapper;
import com.jie.service.ICombinationTopicService;
import com.jie.utils.BeanCopyUtils;
import com.jie.utils.DateRandomTime;
import com.jie.utils.UserUtils;
import com.jie.vo.CombinationTopicVO;
import com.jie.vo.ConditionVO;
import com.jie.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

import static com.jie.constants.RedisConstants.EXERCISE_VIDEO_CACHE;

/**
 * <p>
 * 锻炼视频组合专题 服务实现类
 * </p>
 *
 * @author jie
 * @since 2024-04-21
 */
@Service
public class CombinationTopicServiceImpl extends ServiceImpl<CombinationTopicMapper, CombinationTopic> implements ICombinationTopicService {
    @Autowired
    private VideoMapper videoMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private ThematicCategoryMapper thematicCategoryMapper;
    @Override
    public CombinationTopicDTO getCombinationById(Integer topicId) {
        CombinationTopicDTO cache=null;
        ValueOperations operations = redisTemplate.opsForValue();
        cache=(CombinationTopicDTO)operations.get(EXERCISE_VIDEO_CACHE+topicId);
        if (Objects.isNull(cache)){
            //这里可以直接加一把互斥锁来做缓存击穿的防御 后续也可以来个布隆过滤器来做缓存穿透 //lock()
            //根据资源的排序和id获取到资源的全部信息+排序信息
            //获取当前专题的全部信息
            CombinationTopic combinationTopic = this.baseMapper.selectById(topicId);
            //专题id渲染为资源
            ThematicCategory thematicCategory = thematicCategoryMapper.selectById(combinationTopic.getTopicClassId());
            //组合资源
            cache=BeanCopyUtils.copyObject(combinationTopic,CombinationTopicDTO.class);
            //将视频资源加入
            cache.setVideo(videoMapper.selectById(combinationTopic.getVideoId()));
            cache.setTopicClassInfo(thematicCategory);
            //放入缓存 加入随机时间防止缓存雪崩
            operations.set(EXERCISE_VIDEO_CACHE+topicId,cache, DateRandomTime.getRandomTime(), TimeUnit.SECONDS);
            //unlock()
        }
        return cache;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean addCombinationInfo(CombinationTopicVO combinationTopicVO) {
        if (Objects.isNull(combinationTopicVO)){
            throw new BizException("资源为空,添加失败");
        }
        CombinationTopic combinationTopic = BeanCopyUtils.copyObject(combinationTopicVO, CombinationTopic.class);
        //获取运动大卡统计
        Video video = videoMapper.selectById(combinationTopic.getVideoId());
        //将运动视频数量和大卡放入资源中
        combinationTopic.setTopicConsumable(video.getConsumable());
        UserDetailDTO user = UserUtils.getLoginUser();
        combinationTopic.setAuthorImages(user.getAvatar());
        combinationTopic.setVideoAuthorName(user.getNickname());
        //添加资源
        return this.save(combinationTopic);
    }

    @Override
    public Boolean updateOrDeleteCombinationInfo(CombinationTopicVO combinationTopicVO) {
        //更新当前资源id组
        if (Objects.isNull(combinationTopicVO)){
            throw new BizException("资源为空,更新失败");
        }
        CombinationTopic combinationTopic = BeanCopyUtils.copyObject(combinationTopicVO, CombinationTopic.class);
        //TODO 更新资源没弄好
        return false;
    }

    @Override
    public Boolean deleteCombinationById(Integer topicId) {
        if (Objects.isNull(topicId)) {
            throw new BizException("资源id为空,删除失败");
        }
            //删除资源
            this.removeById(topicId);
            return false;
    }


    @Override
    public PageResult<CombinationTopicDTO> getCombinationPage(ConditionVO conditionVO) {
        Page<CombinationTopic> page = new Page<>(conditionVO.getCurrent(), conditionVO.getSize());
        // 查询日志列表
        Page<CombinationTopic> combinationTopicPage = this.page(page, new LambdaQueryWrapper<CombinationTopic>()
                .like(StringUtils.isNotBlank(conditionVO.getKeywords()), CombinationTopic::getTopicClassId, conditionVO.getKeywords())
                .orderByDesc(CombinationTopic::getTopicPersonCount));
        List<CombinationTopicDTO> combinationTopicDTOS = BeanCopyUtils.copyList(combinationTopicPage.getRecords(), CombinationTopicDTO.class);
        return new PageResult<>(combinationTopicDTOS,  combinationTopicDTOS.size());
    }

}
